package org.fusionide.editors.contentassistants;

import java.util.Vector;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.fusionide.builder.parsers.cfml.Variable;
import org.fusionide.editors.XMLDocument;


public class CFVariablesAssistant implements IContentAssistProcessor {

	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		IDocument document = viewer.getDocument();
		Vector<ICompletionProposal> completionProposals = new Vector<ICompletionProposal>();

		if (document instanceof XMLDocument) {
			XMLDocument cfDocument = (XMLDocument) document;
			String prefix = "";
			try {
				String allData = cfDocument.get(0, offset);

				StringBuffer buf = new StringBuffer();
				for (int i = allData.length() - 1; i >= 0; i--) {
					if (!Character.isJavaIdentifierPart(allData.charAt(i)) && allData.charAt(i) != '.') {
						break;
					}
					buf.insert(0, allData.charAt(i));
				}
				prefix = buf.toString();
			} catch (BadLocationException e) {
				return null;
			}

			cfDocument.parse();
			Vector<Variable> vars = cfDocument.getDocumentParser().getVariables(offset);

			if (vars == null)
				return null;

			for (int i = 0; i < vars.size(); i++) {
				Variable var = vars.get(i);

				String varReplace = var.getName();

				if (!varReplace.toLowerCase().startsWith(prefix.toLowerCase()))
					continue;

				if (var.getType() == Variable.function)
					completionProposals.add(new CompletionProposal(varReplace, offset, 0, varReplace.length() - 1));
				else
					completionProposals.add(new CompletionProposal(
							varReplace,
							offset - prefix.length(),
							prefix.length(),
							varReplace.length(),
							null,
							varReplace + " - " + var.getHint(),
							null,
							null));
			}
		}

		return (ICompletionProposal[]) completionProposals.toArray(new ICompletionProposal[] {});
	}

	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		// System.out.println("computeContextInformation: " + offset);
		return null;
	}

	public char[] getCompletionProposalAutoActivationCharacters() {
		// System.out.println("getCompletionProposalAutoActivationCharacters");
		return new char[] { '.', ' ' };
	}

	public char[] getContextInformationAutoActivationCharacters() {
		// System.out.println("getContextInformationAutoActivationCharacters");
		return null;
	}

	public String getErrorMessage() {
		// System.out.println("getErrorMessage");
		return null;
	}

	public IContextInformationValidator getContextInformationValidator() {
		// System.out.println("getContextInformationValidator");
		return null;
	}

}
